package com.jshop.common.exception;

import com.jshop.common.result.CommonResult;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * 异常处理器
 */
@RestControllerAdvice(value = {"com.jshop"})
public class ApiExceptionHandler {
//    @Value("${alarm.email}")
    private String[] email;
    public static String pojectName = "jshop";
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 自定义异常
     */
    @ExceptionHandler(ApiRRException.class)
    public CommonResult handleRRException(ApiRRException e) {
        logger.info(e.getMsg());
        return CommonResult.failed(e.getMsg(), e.getCode());
    }

//
//    @ExceptionHandler(Exception.class)
//    public CommonResult handException(Exception e) {
//        StringWriter stringWriter = new StringWriter();
//        e.printStackTrace(new PrintWriter(stringWriter));
//        Email mail = new Email();
//        mail.setEmail(email);
//        mail.setSubject(pojectName + ":异常告警邮件通知");
//        mail.setContent(stringWriter.toString());
//        //发送普通邮件
//        mailService.send(mail);
//        logger.error(e.getMessage(), e);
//        return CommonResult.failed("出现错误!通知管理员");
//    }


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResult validException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String msg = "";
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for(FieldError fieldError : fieldErrors) {
            msg = msg + ";" + fieldError.getField() + ":" + fieldError.getDefaultMessage();
        }
        return CommonResult.failed("参数传输不正确：" + msg);
    }

    @ExceptionHandler(AuthorizationException.class)
    public CommonResult AuthorizationException(AuthorizationException e) {
        logger.error(e.getMessage(), e);
        return CommonResult.unauthorized("你没有此权限");

    }

    @ExceptionHandler(DuplicateKeyException.class)
    public CommonResult handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return CommonResult.failed("数据库中已存在该记录");
    }
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public CommonResult duplicateKeyException(SQLIntegrityConstraintViolationException e) {
        logger.error(e.getMessage(), e);
        return CommonResult.failed("数据库中已存在该记录");
    }

//
//    @ExceptionHandler(AuthorizationException.class)
//    public R handleAuthorizationException(AuthorizationException e) {
//        logger.error(e.getMessage(), e);
//        return R.error("没有权限，请联系管理员授权");
//    }
//
//    @ExceptionHandler(Exception.class)
//    public R handleException(Exception e) {
//        logger.error(e.getMessage(), e);
//        return R.error();
//    }
//
//
//    @ExceptionHandler(ApiRRException.class)
//    public Object handleApiRRException(ApiRRException e) {
//        HashMap result = new HashMap();
//        result.put("errno", e.getErrno());
//        result.put("errmsg", e.getErrmsg());
//        return result;
//    }
}
